package com.zlf.controller;

import com.zlf.po.CommonResult;
import com.zlf.po.PointTransaction;
import com.zlf.service.PointService;
import com.zlf.service.PointTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RefreshScope  //开启动态刷新
@RequestMapping("/PointController")
public class PointController {

    @Autowired
    private PointService pointService;
    @Autowired
    private PointTransactionService pointTransactionService;

    @PostMapping("/init/{userId}")    //初始化积分账户 不需前端调用
    public CommonResult initPoint(@PathVariable("userId")String userId){
        try {
            pointService.init(userId);
            return new CommonResult(200,"success",null);
        }catch (Exception e){
            return new CommonResult(500,e.getMessage(),null);
        }
    }

    @PostMapping("/addPoint/{userId}/{amount}")   //增加积分，可以写一个签到加积分，不写也可，在钱包支付时自动增加积分
    public CommonResult addPoint(@PathVariable("userId") String userId,
                                 @PathVariable("amount") Integer amount){
        try {
            pointService.addPoint(userId, amount);
            return new CommonResult(200,"success",null);
        }catch (Exception e){
            return new CommonResult(500,e.getMessage(),null);
        }
    }

    @GetMapping("/getValueById/{userId}")  //查询积分
    public CommonResult getValueById(@PathVariable("userId") String userId){
        try {
            int value =pointService.getValueById(userId);
            return new CommonResult(200,"success",value);
        }catch (Exception e){
            return new CommonResult(500,e.getMessage(),null);
        }
    }
    @PostMapping("/exchange/{userId}/{total}")  //消费积分
    public CommonResult exchange(@PathVariable("userId") String userId,
                         @PathVariable("total") int total) {
        try {
            pointService.dealExchange(userId,total);
            return new CommonResult(200,"success",null);
        }catch (Exception e){
            return new CommonResult(500,e.getMessage(),null);
        }
    }
    @GetMapping("/getPointTrans")  //查询积分明细（详情见util）
    public CommonResult getPointTrans(String userId, Integer index) throws Exception {
        try {
            List<PointTransaction> list =pointTransactionService.getPointTrans(userId, index);
            return new CommonResult(200,"success",list);
        }catch (Exception e){
            return new CommonResult(500,e.getMessage(),null);
        }
    }

}
